﻿using GalaSoft.MvvmLight;
using System.Diagnostics;

namespace SimpleJackInterface.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        public string Welcome
        {
            get
            {
                return "Welcome to MVVM Light";
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            ChatLogViewModel = new ChatLogViewModel(this);

            MySqlService.MySqlServiceClient client = new MySqlService.MySqlServiceClient();

            client.GetDataUsingDataContractAsync(new MySqlService.CompositeType());
            client.GetDataUsingDataContractCompleted += new System.EventHandler<MySqlService.GetDataUsingDataContractCompletedEventArgs>(client_GetDataUsingDataContractCompleted);

            client.GetSimpleJackLogsCompleted += new System.EventHandler<MySqlService.GetSimpleJackLogsCompletedEventArgs>(client_GetSimpleJackLogsCompleted);
            client.GetSimpleJackLogsAsync();

            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real"
            }
        }

        void client_GetDataUsingDataContractCompleted(object sender, MySqlService.GetDataUsingDataContractCompletedEventArgs e)
        {
            throw new System.NotImplementedException();
        }

        void client_GetSimpleJackLogsCompleted(object sender, MySqlService.GetSimpleJackLogsCompletedEventArgs e)
        {
            return;
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}

        
        #region ChatLogViewModel Property
        /// <summary>
        /// The <see cref="ChatLogViewModel" /> property's name.
        /// </summary>
        public const string ChatLogViewModelPropertyName = "ChatLogViewModel";

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private ChatLogViewModel _ChatLogViewModel = null;

        /// <summary>
        /// Gets the ChatLogViewModel property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ChatLogViewModel ChatLogViewModel
        {
            get
            {
                return _ChatLogViewModel;
            }

            set
            {
                if (_ChatLogViewModel == value)
                {
                    return;
                }

                var oldValue = _ChatLogViewModel;
                _ChatLogViewModel = value;

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                RaisePropertyChanged(ChatLogViewModelPropertyName, oldValue, value, true);
            }
        }
        #endregion

    }
}